<?php

//包含头部文件
include './head.php';

// var_dump($_GET);

//获取帖子id 
$getId  =  $_GET['id'];
$username = isset($_COOKIE['username']) ? $_COOKIE['username'] : '';
$currPage = isset($_GET['page']) ? $_GET['page'] : 1;
$uid     = isset($_COOKIE['uid']) ? $_COOKIE['uid'] : '' ;
// var_dump($getId);
// var_dump($uid);
// var_dump($username);

//查询数据库
//查询当前帖子详情 (更新点击数前的表)
$resDetail1 = mySelect($link , '*' , DB_TABLE_DETAIL , "where id = $getId");
$currCid   = $resDetail1[0]['classid'];
$currHits  = $resDetail1[0]['hits'];
$currTitle  = $resDetail1[0]['title'];

//获取用户权限
$resType   = mySelect($link , '*' , DB_TABLE_USER , "where username='$username'");
$userType = $resType[0]['usertype'];

//更新点击数
//当前帖子查询数 + 1
$resHits = myUpdate($link , DB_TABLE_DETAIL , ['hits' => $currHits + 1] , "id=$getId");
//更新点击数后的表 (显示的hit与数据库的hit 一致)
$resDetail = mySelect($link , '*' , DB_TABLE_DETAIL , "where id = $getId");
$authorid = $resDetail[0]['authorid'];
//var_dump($resDetail1);
// var_dump($currCid);
// var_dump($currHits);
// var_dump($resHits);
//var_dump($resDetail);


//通过cid 查出当前的版块名,及其父版块名
$resCategory = mySelect($link , '*' , DB_TABLE_CG ,"where cid=$currCid");
$parentId  = $resCategory[0]['parentid'];
$sonName  = $resCategory[0]['classname'];
//查询父级版块
$resParent = mySelect($link , '*' , DB_TABLE_CG ,"where cid=$parentId");
$parentName  = $resParent[0]['classname'];
// var_dump($resCategory);
// var_dump($parentId);
// var_dump($resParent);


//分页, 并遍历当前帖子的所有回复
$resPage = myPage($link , 3 , 'id' , DB_TABLE_DETAIL , "where tid = $getId and first=0 and isdel=0  order by addtime");
// var_dump($resPage);

/**************************************/
/***	问题:html中未传值时 , 取余会报错	***/
/***	原因:除数不得为0,			***/
/***	解决方法:$num不为空			***/
/**************************************/
//判断分页是否执行成功
if ($resPage) {

	extract($resPage);
	$resReply = $rows;
} 
// var_dump($count);
// var_dump($resReply);
//判断当前页面是否超出范围
if ($currPage > $total) {
	$currPage = $total;
}
if ($currPage < 1) {
	$currPage = 1;
}


//遍历用户表
$resAuthor = mySelect($link , '*' , DB_TABLE_USER);
//var_dump($resAuthor);

//遍历等级表
$resLevel = mySelect($link , '*' , DB_TABLE_LEVEL); 
//var_dump($resLevel);

//根据uid和帖子id订单表
$resOrder = mySelect($link , '*' , DB_TABLE_ORDER , "where tid=$getId  and  uid=$uid");
// var_dump($resOrder);

//判断是否已经购买
/*if (!$resOrder  &&  $uid != $authorid) {

	echo '对不起您尚未购买,';
}
*/

//打包数组
$arr = compact('webSite', 'docRoot' , 'userType' ,  'sonName', 'parentName' , 'currTitle' , 'resDetail' , 'resReply' , 'resAuthor' , 'resLevel' , 'username' , 'currPage' ,'count','total', 'num' , 'offset' ,'prev' ,'next' ,'getId' , 'currCid' , 'uid' , 'resOrder');
//发送
display('detail.html' , $arr);

//包含尾部文件
include './tail.php';